" Vimmb
"
" This vim plugin allows you to connect to twitter and identica microblogging
" services.
"
"

perl << EOF
use Net::Twitter;
sub getTwitterObj() {
	my $user = VIM::Eval('b:twituser');
	my $pass = VIM::Eval('b:twitpass');
	my $identica = VIM::Eval('b:twitnet');
	my $clientname = "vimtwit";

	my $nt = Net::Twitter->new(
		useragent => $clientname,
		source => $clientname,
		clientname => $clientname,
		identica => $identica,
		username => $user,
		password => $pass
	);

	return $nt;
}

sub twitter_post() {
	my @pos = $curwin->Cursor();
	my $toPost = $curbuf->Get($pos[0]);

	my $nt = getTwitterObj();
	$nt->update($toPost);
}

sub twitter_friends() {
	my @pos = $curwin->Cursor();
	my $nt = getTwitterObj();

	my $statuses = $nt->friends_timeline();
	my $i = $pos[0];
	for my $status ( @$statuses ) {
		$curbuf->Append($i, "[$status->{user}{screen_name}] $status->{text}");
		$curbuf->Append($i+1, "--------------------------------------------------------------------------------");
		$i = $i + 2;
	}
}

EOF

"twitter maps
nmap <silent> <Leader>tf :call TwitterFriends()<CR>
nmap <silent> <Leader>tt :call TwitterTimeline()<CR>
nmap <silent> <Leader>tp :call TwitterPost()<CR>
nmap <silent> <Leader>tc :call TwitterClearUser()<CR>
nmap <silent> <Leader>tn :call TwitterSetNetwork()<CR>

fu! TwitterSetNetwork()
		let b:twitnet = input("Network (0 - twitter, 1 - identica):")	
endf
fu! TwitterSetUser() 
	if !exists('b:twitnet') || b:twitnet == ''
		call TwitterSetNetwork()
	endif
    if !exists('b:twituser') || b:twituser == '' 
        let b:twituser = input("Twitter username: ") 
        let b:twitpass = inputsecret("Twitter password: ") 
    endif 
endf 
fu! TwitterClearUser() 
    let b:twituser = '' 
    let b:twitpass = '' 
	let b:twitnet  = ''
endf 
fu! TwitterFriends()
	call TwitterSetUser()
	perl twitter_friends()
endf
fu! TwitterPost() 
	call TwitterSetUser()
	perl twitter_post()
endf 
